home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6898 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  86 lines

  1. Path: news.nas.com!news
  2. From: gldnspud@kali.nas.com (Matt Scott)
  3. Newsgroups: comp.lang.c
  4. Subject: Function Prototype placement in source files
  5. Date: Fri, 16 Feb 1996 01:58:30 GMT
  6. Organization: Everyone's Software
  7. Message-ID: <4g0obg$78b@barad-dur.nas.com>
  8. NNTP-Posting-Host: kali.nas.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I have a question of style for everyone knowledgable and interested
  12. in the subject.
  13.  
  14. My C Programming prof. at the community college, as well as the book
  15. he teaches with, put function prototypes inside main(). I didn't write
  16. it down or print it out, but here is what I remember of an example he
  17. wrote in the past to illustrate functions and static variables:
  18.  
  19. (I'll try to emulate his brace style as much as I can -- sometimes it
  20. changes. Also, I don't know why he chose to use a float in this
  21. example,
  22. but he just did).
  23.  
  24. #include <stdio.h>
  25.  
  26. main()
  27.   {
  28.     float x;
  29.     float one(void);                /* 1 */
  30.     void two(void);                 /* 2 */
  31.  
  32.     x = one();
  33.     printf("i is now %.2f\n", x);
  34.  
  35.     two();
  36.   }
  37.  
  38. /* --------------------------------------------------------- */
  39.  
  40. float one(void)
  41.   {
  42.     static float i = 10;
  43.  
  44.     i += 5;
  45.     return i;
  46.   }
  47.  
  48. /* --------------------------------------------------------- */
  49.  
  50. void two(void)
  51.   {
  52.     float f;
  53.     int g;
  54.     float one(void);                /* 3 */
  55.  
  56.     for (g = 0; g < 6; ++g)
  57.       {
  58.         f = one();
  59.         printf("i is now %.2f\n", f);
  60.       }
  61.  
  62.     return;
  63.   }
  64.  
  65. /**************************************************************/
  66.  
  67. My question is this: is there a compelling reason not to prototype
  68. functions above all of the functions using it, such as:
  69.  
  70. float one(void);
  71. void two(void);
  72.  
  73. main()
  74. ...
  75.  
  76. In almost every instance of prototypes I've seen, they are declared
  77. outside of any function -- mostly above all other functions, and in
  78. a header file most often.
  79.  
  80. Is there a good reason to do it the way he and the book do it?
  81.  
  82. Thanks in advance for any thoughtful answers.
  83.  
  84.  
  85.  
  86.